package cn.jiedh.app.meiweidk.web;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ProgressBar;


public class AutoStepProgressBar extends ProgressBar implements Animator.AnimatorListener, ILoading {
    private static final int FIRST_STEP_MAX_VAL = 800;
    private static final int SECOND_STEP_MAX_VAL = 980;
    private static final int FIRST_STEP_DURATION = 4000;
    private static final int SECOND_STEP_DURATION = 6000;
    private static final int HIDE_DELAY = 100;

    private ValueAnimator mProgressAnimator;

    private boolean mIsStop = false;
    private Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            setProgress(0);
            setVisibility(View.GONE);
        }
    };

    public AutoStepProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public AutoStepProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoStepProgressBar(Context context) {
        this(context, null);
    }

    @Override
    public void start() {
        // if (mProgressAnimator == null || !mProgressAnimator.isRunning()) {
        if (mProgressAnimator == null) {
            removeCallbacks(mHideRunnable);
            setVisibility(View.VISIBLE);
            setProgress(0);
            mProgressAnimator = ObjectAnimator.ofInt(this, "progress", 0, FIRST_STEP_MAX_VAL);
            mProgressAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
            mProgressAnimator.setDuration(FIRST_STEP_DURATION);
            mProgressAnimator.addListener(this);
            mProgressAnimator.start();

            mIsStop = false;
        }
    }

    @Override
    public void stop() {
        stop(getMax());
    }

    public void stop(int finalProgress) {
        stopProgressAnimation();
        setProgress(finalProgress);
        postDelayed(mHideRunnable, HIDE_DELAY);
    }

    @Override
    public void stopImmediately() {
        stopProgressAnimation();
        setProgress(0);
        setVisibility(View.GONE);
    }

    private void stopProgressAnimation() {
        removeCallbacks(mHideRunnable);
        if (mProgressAnimator != null) {
            mProgressAnimator.removeAllListeners();
            mProgressAnimator.cancel();
            mProgressAnimator = null;

            if (!mIsStop) {
                mIsStop = true;
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        stopProgressAnimation();
        super.onDetachedFromWindow();
    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
        mProgressAnimator.removeAllListeners();
        mProgressAnimator = ObjectAnimator.ofInt(this, "progress", FIRST_STEP_MAX_VAL, SECOND_STEP_MAX_VAL);
        mProgressAnimator.setDuration(SECOND_STEP_DURATION);
        mProgressAnimator.start();
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }
}
